package org.example.validation;


import lombok.Getter;
import lombok.Setter;
import org.example.exception.ValidateException;

import java.util.ArrayList;
import java.util.List;

//校验上下文 存储校验信息  并控制是否继续校验  从属于责任链chain
public class ValidatorContext {
    private final List<String> errorMessages = new ArrayList<>();

    private boolean stop = false;

    @Setter
    @Getter
    private  Object value;


    public void addErrorMessage(String errorMessage) {
        errorMessages.add(errorMessage);
    }


    //抛出异常
    public void throwExceptionIfNeed() throws ValidateException {
        if (!errorMessages.isEmpty()) {
            throw new ValidateException(errorMessages.toString());
        }
    }


    //停止下面的校验 直接改标志位
    public void stopChain() {
        stop = true;
    }

    public boolean shouldStop() {
        return stop;
    }


    private  int index = 0;
    public void doNext(Object value){
        index++;
        this.value = value;
    }

    public int currentIndex(){
        return index;
    }


}
